﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharepointSolutionInstaller.Views;
using SharepointSolutionInstaller.Resources;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SharepointSolutionInstaller.Utils.Configuration;

namespace SharepointSolutionInstaller.Utils.SolutionDeployment
{
    public class ActivateFarmFeatureCommand : FeatureCommand
    {
        List<Guid?> currentFeatureIds;
        public ActivateFarmFeatureCommand(InstallProcess parent, List<Guid?> FeatureIds)
            : base(parent) {

                this.currentFeatureIds = FeatureIds;
        }

        internal override string Description
        {
            get
            {
                // Modif JPI - Début
                return CommonUIStrings.activateFarmFeatureCommand;
                // Modif JPI - Fin
            }
        }

        protected internal override bool Execute()
        {
            try
            {
                // Modif JPI - Début
                List<Guid?> featureIds = currentFeatureIds;
                if (featureIds != null && featureIds.Count > 0)
                {
                    foreach (Guid? featureId in featureIds)
                    {
                        if (featureId != null)
                        {
                            SPFeature feature = SPWebService.AdministrationService.Features.Add(featureId.Value, true);
                        }
                    }
                }
                return true;
                //// Modif JPI - Fin
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        protected internal override bool Rollback()
        {
            DeactivateFeature(currentFeatureIds);
            return true;
        }
    }
}
